﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace SkypeCore
{
	public class ContactsManager
	{
		private readonly string _databasePath;

		public ContactsManager(string databasePath)
		{
			_databasePath = databasePath;
		}

		public IEnumerable<SkypeContact> Get()
		{
			using (var connection = DatabaseConnection.Get(_databasePath))
			{
				const string commandText = "select id, skypename, fullname from contacts where skypename <> 'null'";
				using (var command = new SQLiteCommand(commandText, connection))
				{
					using (var reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							var contact = new SkypeContact
							              	{
							              		SkypeName = reader.GetString(1),
							              		FullName = reader.GetValue(2) as string,
							              		Id = reader.GetInt32(0)
							              	};
							yield return contact;
						}
						
					}
				}
			}
		}


		public IEnumerable<string> GetSelfName()
		{
			using (var connection = DatabaseConnection.Get(_databasePath))
			{
				const string commandText = "select skypename from accounts";
				using (var command = new SQLiteCommand(commandText, connection))
				{
					using (var reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							yield return reader.GetString(0);
						}
					}
				}
			}
		}
	}
}
